package com.insta.playdroid;

import java.util.List;

import adapteurMediaStore.AdapteurSongMediaStore;
import adapteurMediaStore.IAdapteurMediaStore;
import adapteurMediaStore.ISongUtils;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.insta.adapterList.SongAdapter;
import com.insta.playdroid.metier.domain.Song;

public class SongsActivity2 extends MediaPlayerNewListActivity {

	public static List<Song> songs;
	private int position;
	private IAdapteurMediaStore<Song> adapteurMediaStore;
	private ISongUtils songUtils;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.allsong);

		// récup l'instance du player
		player = PlayerFactory.create(0, null, this);

		// charger la liste des albums (id, titre, art)
		adapteurMediaStore = new AdapteurSongMediaStore(getBaseContext());
		songs = adapteurMediaStore.collectAllValuesFromDB();

		/* charger les items albums */

		ListView lv = (ListView) findViewById(android.R.id.list);

		// menu contextuel
		registerForContextMenu(lv);

		lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> av, View v, int pos,
					long id) {
				onLongListItemClick(v, pos, id);
				return false;
			}

			private void onLongListItemClick(View v, int pos, long id) {
				position = pos;

			}
		});

		lv.setTextFilterEnabled(true);
		SongAdapter adapter = new SongAdapter(this, songs);
		lv.setAdapter(adapter);
		adapter.notifyDataSetChanged();

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,

	ContextMenuInfo menuInfo) {

		super.onCreateContextMenu(menu, v, menuInfo);

		MenuInflater inflater = getMenuInflater();

		inflater.inflate(R.menu.contextclickmusic, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		// On récupère l'id de l'item et on le compare

		switch (item.getItemId()) {

		case R.id.itemaddtoplaylist:

			Toast.makeText(this, "Not implemented yet", Toast.LENGTH_SHORT)
					.show();

			return true;

		case R.id.itemdelete:
			// songUtils = new SongUtils(getBaseContext());
			// songUtils.deleteTrack(getBaseContext(), songs.get(position)
			// .getLocation() + "/" + songs.get(position).getTitle());

			Toast.makeText(this, "song deleted", Toast.LENGTH_SHORT).show();

			return true;

		}

		return super.onContextItemSelected(item);

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		// update le player
		player.setSongs(songs);
		player.setSong(position);
		player.setContext(this);
		// démarer player
		player.startPlayerActivity();

	}

}
